package com.pcbly.web.action.crm;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.crm.BxdBean;
import com.pcbly.web.bean.crm.BxdProductBean;
import com.pcbly.web.business.crm.BxdProductService;
import com.pcbly.web.business.crm.BxdService;
import com.pcbly.web.business.depart.DepartService;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.form.AnalyseCommonDataExt;

/**
 * @author carl.song Copyright (c) 2010 Lightsoft, International. All rights
 *         reserved.
 * 
 */

public class BxdAction extends BaseAction<BxdBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1880760484373631787L;

	private BxdService bxdService;

	private BxdProductService bxdProductService;
	
	private DepartService departService;

	private BxdProductBean bxdProductBean;

	private BxdBean bxd;

	private String orderBy;

	private Page<BxdBean> pages;

	private boolean asc = true;

	
	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}

	public void setBxdProductService(BxdProductService bxdProductService) {
		this.bxdProductService = bxdProductService;
	}

	public BxdProductBean getBxdProductBean() {
		return bxdProductBean;
	}

	public void setBxdProductBean(BxdProductBean bxdProductBean) {
		this.bxdProductBean = bxdProductBean;
	}

	public void setBxdService(BxdService bxdService) {
		this.bxdService = bxdService;
	}

	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public BxdBean getBxd() {
		return bxd;
	}

	public Page<BxdBean> getPages() {
		return pages;
	}

	public void setPages(Page<BxdBean> pages) {
		this.pages = pages;
	}

	public void setBxd(BxdBean bxd) {
		this.bxd = bxd;
	}

	public void selectEngineers(){
		
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		bxd = bxdService.getBxdBeanById(getId());
		extendCustomer = bxdService
				.findBxdBeanExtView("t_crm_bxd_ext", getId());

		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String check(String serialNum)throws Exception{
		try {
			bxdProductBean=bxdProductService.getBxdProductBeanByBxdId(getId());
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}
	public String desc() throws Exception {
		try {
			bxdProductBean=bxdProductService.getBxdProductBeanByBxdId(getId());
			return SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ERROR;
		}
	}

	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		bxd = bxdService.getBxdBeanById(getId());
		extendCustomer = bxdService
				.findBxdBeanExtEdit("t_crm_bxd_ext", getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}

	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();

		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<BxdBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = bxdService.findBxdBeanByCond(bxd, pages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}

	public String add() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		bxd = new BxdBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		extendCustomer = bxdService.findBxdBeanExtCreate("t_crm_bxd_ext");
		return super.add();
	}

	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bxd.getId());
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_crm_bxd_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);

		try {
			if (bxd.getId() == null || "".equals(bxd.getId().trim())) {// new
				bxd.setCreateDate(new Date());
				bxd.setCreateUserId(getUserId());
				bxd.setRecycled("0");
				bxd = bxdService.saveBxdBean(bxd);
				ext.setId(bxd.getId());
				String sql = ext.execute();
				if (!(sql == null || "".equals(sql.trim()))) {
					bxdService.saveBxdBeanExt(sql);
				}
				setMyMessage(getText("entity.saved"));
			} else {// update
				bxdService.updateBxdBean(bxd);

				boolean iflag = bxdService.getHasBxdBeanExtData(
						"t_crm_bxd_ext", bxd.getId());
				ext.setHaveData(iflag);
				ext.setId(bxd.getId());
				String sql = ext.execute();
				if (!(sql == null || "".equals(sql.trim()))) {
					bxdService.saveBxdBeanExt(sql);
				}

				setMyMessage(getText("entity.updated"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		extendCustomer = bxdService.findBxdBeanExtEdit("t_crm_bxd_ext", bxd
				.getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}

	public String dwrSave(BxdBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try {
			if (bean.getId() == null || "".equals(bean.getId())) {// new
				bxdService.saveBxdBean(bean);
			} else {// update
				bxdService.updateBxdBean(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}

	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if (getId().indexOf(",") >= 0) {
			bxdService.removeBxdBeanByIds(getId());
		} else {
			bxdService.removeBxdBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		bxd = null;
		return super.delete();
	}

	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if (id.indexOf(",") >= 0) {
			bxdService.removeBxdBeanByIds(id);
		} else {
			bxdService.removeBxdBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}

}